/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.1
import WebFeeds 1.0
import NewsBlur 1.0
import "../../../../shared/qml/UIConstants.js" as UIConstants
import "../../../../shared/qml" as Shared
import "../delegates" as Delegates
import "../misc" as Misc
import "../buttons" as Buttons

Shared.CommonPage {
    id: root
    property variant service: rootItem.service
    property bool __serviceBusy: service ? service.busy : false
    property bool userInteractionEnabled: !root.__serviceBusy && !(root.pageStack && root.pageStack.busy)
                                          && ((root.rootItem != null) && !root.rootItem.movingBusy && !root.rootItem.deletingBusy)
    contentsBusy: (((root.rootItem != null) && root.rootItem.childrenBusy) || root.__serviceBusy)

    function pageForItem(item) {
        var itemType = item.itemType;
        if(itemType == NewsBlurItem.FolderItem)
            return "/plugins/newsblur/qml/pages/FolderPage.qml";
        else if ((itemType == NewsBlurItem.FeedItem) ||
                 (itemType == NewsBlurItem.StarredGroup) ||
                 (itemType == NewsBlurItem.EverythingGroup))
            return "/plugins/newsblur/qml/pages/FeedPage.qml"
        else if(itemType == NewsBlurItem.StoryItem)
            return "/plugins/newsblur/qml/pages/StoryPage.qml"
        else
            return "";
    }

    contents: Shared.CommonListPageContents {
        id: listPageContents
        rootItem: root.rootItem
        rootIndex: webFeedsApp.pageItemModel.index(root.rootItem);
        busy: root.contentsBusy
        emptyListText: qsTr("No feeds")
        pageHeader: Shared.CommonPageHeader {
            titleText: root.rootItem.title
            subTitleColor: UIConstants.HEADER_SUBTITLE_COLOR
            titleColor: "white"
            backgrounColor: UIConstants.HIGHLIGHT_COLOR
            titleFont: UIConstants.FONT_FAMILY_LIGHT
            rightHelperItem: Misc.HeaderCountBubble {
                busy: root.rootItem.busy
                neutralCount: root.rootItem.unreadCount
                negativeCount: root.rootItem.negUnreadCount
                positiveCount: root.rootItem.posUnreadCount
            }
            Shared.Separator {
                id: separator
                anchors.bottom: parent.bottom
            }
        }
        cacheBuffer: UIConstants.DEFAULT_DELEGATE_HEIGHT * 64
        delegate: Delegates.NewsBlurPageListDelegate {
            onReleased: {
                createPageForItem(listPageContents.itemForRow(row));
            }
            onHolded: showContextMenuForItem(listPageContents.itemForRow(row));
        }
    }
    tools: Shared.CommonToolBarLayout {
        enabled: root.userInteractionEnabled
        Shared.BackToolButton {pageItem: root.rootItem; page: root}
        Buttons.MarkEverythingAsReadToolButton {pageItem: root.rootItem}
        Buttons.AddToolButton {pageItem: root.rootItem;}
        Shared.MenuToolButton {pageItem: root.rootItem; page: root}
    }
}
